package cn.afactory.tygz.ui.base.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.NearByUserInfo;
import cn.afactory.tygz.ui.base.entity.CommunityInfo;
import cn.afactory.tygz.ui.base.entity.PeopleInfo;
import cn.afactory.tygz.ui.base.utils.DateUtil;
import cn.afactory.tygz.ui.base.utils.StringUtils;
import cn.afactory.tygz.ui.base.utils.TCEventHelper;

/**
 * 附近的人
 */
public class NearByPeoplesAdapter extends BaseAdapter {

    private int mWidth;
    private int mHeight;

    private Context mContext;

    private List<NearByUserInfo> mPeopleInfoList = new ArrayList<NearByUserInfo>();

    public NearByPeoplesAdapter(Context context, List<NearByUserInfo> listData, int width, int height) {
        mWidth = width;
        mHeight = height;
        mContext = context;
        mPeopleInfoList = listData;
    }

    public void setmPeopleInfoList(List<NearByUserInfo> dataList) {
        mPeopleInfoList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mPeopleInfoList != null ? mPeopleInfoList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        if (mPeopleInfoList != null && mPeopleInfoList.size() > 0) {
            return mPeopleInfoList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NearByPeoplesViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fg_nearby_people_item, null);
            viewHolder = new NearByPeoplesViewHolder();
            viewHolder.nearByPeopleIcon = (ImageView) convertView.findViewById(R.id.nearby_people_icon);
            viewHolder.nearByPeopleName = (TextView) convertView.findViewById(R.id.nearby_people_name);
            viewHolder.nearByPeopleGender = (ImageView) convertView.findViewById(R.id.nearby_people_gender);
            viewHolder.nearByPeopleAge = (TextView) convertView.findViewById(R.id.nearby_people_age);
            viewHolder.nearByPeopleDistance = (TextView) convertView.findViewById(R.id.nearby_people_distance);
            viewHolder.nearByPeopleCity = (TextView) convertView.findViewById(R.id.nearby_people_city);
            viewHolder.splitLine = (View)convertView.findViewById(R.id.split_city_company);
            viewHolder.nearByPeopleCompany = (TextView) convertView.findViewById(R.id.nearby_people_company);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (NearByPeoplesViewHolder) convertView.getTag();
        }

        int nwidth = mWidth > 0 ? mWidth : mContext.getResources().getDimensionPixelOffset(R.dimen.tv_scan_margin_top);
        if (mPeopleInfoList.get(position).userIcon != null && mPeopleInfoList.get(position).userIcon.length() > 0) {
            BaseImgView.loadimg(viewHolder.nearByPeopleIcon, mPeopleInfoList.get(position).userIcon, R.drawable.ic_user_default,
                    R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            viewHolder.nearByPeopleIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }

        if (mPeopleInfoList.get(position).sex == 0){
            viewHolder.nearByPeopleGender.setImageResource(R.drawable.tag_male);
        } else {
            viewHolder.nearByPeopleGender.setImageResource(R.drawable.tag_female);
        }

        viewHolder.nearByPeopleName.setText(null == mPeopleInfoList.get(position).nickName ? "" : mPeopleInfoList.get(position).nickName);
        viewHolder.nearByPeopleAge.setText(DateUtil.getAgeByNorth(mPeopleInfoList.get(position).birth) + "岁");
        viewHolder.nearByPeopleDistance.setText(StringUtils.formartDistance(mPeopleInfoList.get(position).distance));
        viewHolder.nearByPeopleCity.setText(null == mPeopleInfoList.get(position).cityName ? "上海人" : mPeopleInfoList.get(position).cityName + "人");
        if (!TextUtils.isEmpty(mPeopleInfoList.get(position).companyName)) {
            viewHolder.nearByPeopleCompany.setText(mPeopleInfoList.get(position).companyName);
            viewHolder.splitLine.setVisibility(View.VISIBLE);
        } else {
            viewHolder.nearByPeopleCompany.setText("");
            viewHolder.splitLine.setVisibility(View.GONE);
        }
        return convertView;
    }

    class NearByPeoplesViewHolder {
        ImageView nearByPeopleIcon;
        TextView nearByPeopleName;
        ImageView nearByPeopleGender;

        TextView nearByPeopleAge;
        TextView nearByPeopleDistance;
        TextView nearByPeopleCity;
        View splitLine;
        TextView nearByPeopleCompany;
    }
}
